home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / FROMUTS / UNIXLIB37B / src / c / ctime < prev    next >
Text File  |  1992-02-14  |  4KB  |  160 lines

  1. #ifdef __STDC__
  2. static char sccs_id[] = "@(#) ctime.c 2.0 "__DATE__" HJR";
  3. #else
  4. static char sccs_id[] = "@(#) ctime.c 2.0 26/9/90 HJR";
  5. #endif
  6.  
  7. /* ctime.c (c) Copyright 1990 H.Rogers */
  8.  
  9. #include <time.h>
  10. #include <stdio.h>
  11. #include <ctype.h>
  12.  
  13. static char *__tdays[] =
  14.   { "Sun","Mon","Tue","Wed","Thu","Fri","Sat" };
  15. static char *__tdayl[] =
  16.   { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" };
  17. static char *__tmonths[] =
  18.   { "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" };
  19. static char *__tmonthl[] =
  20.   { "January","February","March","April","May","June","July","August",
  21.     "September","October","November","December" };
  22.  
  23. /* standard representations (take care to avoid making
  24.  * strftime() call itself recursively ad infinitum) */
  25.  
  26. static char *__dtrep = "%a %b %d %H:%M:%S %Y";
  27. static char *__drep = "%a %b %d %Y";
  28. static char *__trep = "%H:%M:%S";
  29.  
  30. #ifdef __STDC__
  31. char *asctime(register const struct tm *t)
  32. #else
  33. char *asctime(t)
  34. register const struct tm *t;
  35. #endif
  36. {
  37. static char _buf[26];
  38. register char *buf = _buf;
  39.  
  40. buf += strftime(buf,24,__dtrep,t); *buf++ = '\n'; *buf++ = 0;
  41.  
  42. return(_buf);
  43. }
  44.  
  45. #ifdef __STDC__
  46. char *ctime(register const time_t *tp)
  47. #else
  48. char *ctime(tp)
  49. register const time_t *tp;
  50. #endif
  51. {
  52. return(asctime(localtime(tp)));
  53. }
  54.  
  55. #ifdef __STDC__
  56. size_t strftime(register char *buf,register size_t max,
  57.     register const char *fmt,register const struct tm *t)
  58. #else
  59. size_t strftime(buf,max,fmt,t)
  60. register char *buf;
  61. register size_t max;
  62. register const char *fmt;
  63. register const struct tm *t;
  64. #endif
  65. {
  66. register int i = max;
  67.  
  68. while (*fmt && i)
  69.   {
  70.   if (*fmt != '%' || *++fmt == '%')      /* left to right evaluation */
  71.     { *buf++ = *fmt++,i--; continue; }
  72.  
  73.     {
  74.     register char *s;
  75.     register int j;
  76.  
  77.     switch (*fmt)
  78.       {
  79.       case 'a':
  80.     s = __tdays[t->tm_wday]; scp: while((*buf = *s) && i) buf++,s++,i--;
  81.     break;
  82.       case 'A':
  83.     s = __tdayl[t->tm_wday];
  84.     goto scp;
  85.       case 'b':
  86.     s = __tmonths[t->tm_mon];
  87.     goto scp;
  88.       case 'B':
  89.     s = __tmonthl[t->tm_mon];
  90.     goto scp;
  91.       case 'c':
  92.     j = strftime(buf,i,__dtrep,t); buf += j,i -= j;
  93.     break;
  94.       case 'd':
  95.     if (i >= 2) sprintf(buf,"%2d",t->tm_mday); buf += 2,i -= 2;
  96.     break;
  97.       case 'H':
  98.     if (i >= 2) sprintf(buf,"%.2d",t->tm_hour); buf += 2,i -= 2;
  99.     break;
  100.       case 'I':
  101.     j = t->tm_hour; if (j > 12) j -= 12;
  102.     if (i >= 2) sprintf(buf,"%2d",j); buf += 2,i -= 2;
  103.     break;
  104.       case 'j':
  105.     if (i >= 3) sprintf(buf,"%3d",t->tm_yday); buf += 3,i -= 3;
  106.     break;
  107.       case 'm':
  108.     if (i >= 2) sprintf(buf,"%2d",t->tm_mon); buf += 2,i -= 2;
  109.     break;
  110.       case 'M':
  111.     if (i >= 2) sprintf(buf,"%.2d",t->tm_min); buf += 2,i -= 2;
  112.     break;
  113.       case 'p':
  114.     s = (t->tm_hour > 12) ? "PM" : "AM";
  115.     goto scp;
  116.       case 'S':
  117.     if (i >= 2) sprintf(buf,"%.2d",t->tm_sec); buf += 2,i -= 2;
  118.     break;
  119.       case 'U':
  120.     j = t->tm_yday;
  121.     if (j > 2) j += (4 - t->tm_wday);
  122.     if (i >= 2) sprintf(buf,"%2d",j / 7); buf += 2,i -= 2;
  123.     break;
  124.       case 'w':
  125.     *buf++ = t->tm_wday + '0'; i--;
  126.     break;
  127.       case 'W':
  128.     j = t->tm_yday;
  129.     if (j > 2) j += (5 - ((j = t->tm_wday) ? j : 7));
  130.     if (i >= 2) sprintf(buf,"%2d",j / 7); buf += 2,i -= 2;
  131.     break;
  132.       case 'x':
  133.     j = strftime(buf,i,__drep,t); buf += j,i -= j;
  134.     break;
  135.       case 'X':
  136.     j = strftime(buf,i,__trep,t); buf += j,i -= j;
  137.     break;
  138.       case 'y':
  139.     if (i >= 2) sprintf(buf,"%2d",t->tm_year); buf += 2,i -= 2;
  140.     break;
  141.       case 'Y':
  142.     if (i >= 4) sprintf(buf,"%4d",t->tm_year + 1900); buf += 4,i -= 4;
  143.     break;
  144.       case 'Z':
  145.     s = (char *)t->tm_zone;
  146.     goto scp;
  147.     break;
  148.       default:
  149.     *buf++ = *fmt,i--;
  150.     break;
  151.       }
  152.     fmt++;
  153.     }
  154.   }
  155.  
  156. *buf = 0;
  157.  
  158. return((*fmt) ? 0 : max - i);
  159. }
  160.